package org.erepublik.company.controllers;

import java.util.Arrays;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.erepublik.company.Location;
import org.erepublik.company.charts.ChartDataSet;
import org.erepublik.company.charts.ChartFactory;
import org.erepublik.company.comparators.FoodComparatorByWellness;
import org.erepublik.company.product.Food;
import org.erepublik.company.searchCriteria.CompareCharts;
import org.erepublik.company.searchCriteria.EQualityFilter;
import org.erepublik.company.utils.Utils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/countryCharts.html")
public class CountryChartController extends AbstractController {
    
    @RequestMapping(method = RequestMethod.GET)
    public String showForm(HttpServletRequest request, ModelMap model) {
        fillModelMap(model);
        Location country = null;
        if (request.getParameter("country") != null && !request.getParameter("country").equals("")) {
            country = Utils.getLocationFromString( request.getParameter("country")  );
        }
 
        String attribute = request.getParameter("attribute");
        
        String title = attribute+" in "+country.getName();
        
        ChartDataSet data = countryDataService.getDataCollctor("currency").getData(country, attribute);
        model.addAttribute("weekChart", ChartFactory.getSimpleChart(
                    new Date().getTime()-1000*60*60*24*7, data, title));
        model.addAttribute("compareCharts", new CompareCharts());
        model.addAttribute("title", title);

        return "countryCharts";
    }
    
    
    @RequestMapping(method = RequestMethod.POST)
    public String showList(HttpServletRequest request, ModelMap model, @ModelAttribute("compareCharts") CompareCharts criteria) {
        fillModelMap(model);
        
        String attribute = request.getParameter("attribute");
        Location country = null;
        if (request.getParameter("country") != null && !request.getParameter("country").equals("")) {
            country = Utils.getLocationFromString( request.getParameter("country")  );
        }
        ChartDataSet data1 = countryDataService.getDataCollctor("currency").getData(country, attribute);
        ChartDataSet data2 = countryDataService.getDataCollctor("currency").getData( Utils.getLocationFromString( criteria.getLocation().toString() ), attribute);
        
        String title = attribute+" comparision beetwen "+country.getName()+" (red) and "+criteria.getLocation().getName()+" (blue)";
        model.addAttribute("compareCharts", criteria);
        model.addAttribute("weekChart", ChartFactory.getComparisonChart(
                new Date().getTime()-1000*60*60*24*7, data1, data2,  title));
        model.addAttribute("title", new CompareCharts());
        model.addAttribute("title", title);
        
        return "countryCharts";
    }
    
}
